package com.fanling.venus.project.service;

import com.fanling.venus.common.constant.ProjectContants;
import com.fanling.venus.common.utils.StringUtils;
import com.fanling.venus.project.domain.EpointProject;
import com.fanling.venus.project.domain.EpointWeeklyWork;
import com.fanling.venus.project.mapper.EpointWeeklyWorkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EpointWeeklyWeekService {
    @Autowired
    private EpointWeeklyWorkMapper epointWeeklyWorkMapper;

    @Autowired
    private EpointProjectService epointProjectService;

    /**
     * 展示列表
     *
     * @param epointWeeklyWork
     * @return
     */
    public List<EpointWeeklyWork> selectWeeklyWorkList(EpointWeeklyWork epointWeeklyWork) {
        return epointWeeklyWorkMapper.selectWeeklyWorkList(epointWeeklyWork);
    }

    /**
     * 获取每周工作信息
     * @param workId
     * @return
     */
    public EpointWeeklyWork selectWeeklyWorkById(Long workId) {
        return epointWeeklyWorkMapper.selectWeeklyWorkById(workId);
    }

    /**
     * 更新 工作任务
     *
     * @param epointWeeklyWork
     * @return
     */
    public int updateWeeklyWork(EpointWeeklyWork epointWeeklyWork) {
        return epointWeeklyWorkMapper.updateWeeklyWork(epointWeeklyWork);
    }

    /**
     * 新增工作任务
     *
     * @param epointWeeklyWork
     * @return
     */
    public int insertWeeklyWork(EpointWeeklyWork epointWeeklyWork) {
        EpointProject project = epointProjectService.selectProjectById(epointWeeklyWork.getProjectId());
        if (project != null) {
            epointWeeklyWork.setProjectName(project.getProjectName());
        } else {
            epointWeeklyWork.setProjectName("无项目名称");
        }
        return epointWeeklyWorkMapper.insertWeeklyWork(epointWeeklyWork);
    }

    /**
     * 删除 工作任务
     *
     * @param workId
     * @return
     */
    public int deleteWeeklyWorkById(Long workId) {
        return epointWeeklyWorkMapper.deleteWeeklyWorkById(workId);
    }


    /**
     * 校验项目名称+工作标题是否唯一
     *
     * @param epointWeeklyWork 项目信息
     * @return 结果
     */
    public String checkProjectNameAndWorkTitleUnique(EpointWeeklyWork epointWeeklyWork) {
        Long workId = StringUtils.isNull(epointWeeklyWork.getWorkId()) ? -1L : epointWeeklyWork.getWorkId();
        EpointWeeklyWork info = epointWeeklyWorkMapper.checkProjectNameAndWorkTitleUnique(epointWeeklyWork.getProjectName(), epointWeeklyWork.getWorkTitle());
        if (StringUtils.isNotNull(info) && info.getWorkId().longValue() != workId) {
            return ProjectContants.PROJECT_NAME_NOT_UNIQUE;
        }
        return ProjectContants.PROJECT_NAME_UNIQUE;
    }
}
